package com.wyp168.leetcode.listnode;

/**
 * @ClassName MergeTwoLists
 * @Description TODO
 * @Author wyp168
 * @Date 2022/6/21 21:26
 */
public class MergeTwoLists {
    public static void main(String[] args) {

        ListNode ln1 = new ListNode(1);
        ListNode ln2 = new ListNode(3);
        ListNode ln3 = new ListNode(5);
        ln1.next = ln2;
        ln2.next = ln3;

        ListNode ln11 = new ListNode();

        System.out.println(mergeTwoLists(ln1, ln11));
    }


    public static ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        if (list1 == null) {
            return list2;
        }
        if (list2 == null) {
            return list1;
        }

        ListNode curr = new ListNode(0);
        ListNode next = curr;

        //循环遍历
        while(list1 != null && list2 != null) {
            //判断链表的值
            if (list1.val > list2.val) {
                next.next = list2;
                list2 = list2.next;
            } else {
                next.next = list1;
                list1 = list1.next;
            }
            next = next.next;
        }

        next.next = list1 == null ? list2 : list1;



        return curr.next;
    }
}